package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ViewUtil;
import com.netease.nim.uikit.cache.TeamDataCache;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.team.TeamService;
import com.netease.nimlib.sdk.team.model.Team;

/**
 * Created by Administrator on 2017/9/18 0018.
 */

public class FamilyPermissionActivity extends BaseRequestActivity {

    CheckBox cb_family_feed, cb_family_shortlive, cb_family_live, cb_family_message;
    private String requestPermissionCommand;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, FamilyPermissionActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_permission;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ViewUtil.setForgroundRadius($(R.id.content));
        setTitle(R.string.str_family_permission_title);

        cb_family_feed = ViewUtil.findView(this, R.id.cb_family_feed);
        cb_family_shortlive = ViewUtil.findView(this, R.id.cb_family_shortlive);
        cb_family_live = ViewUtil.findView(this, R.id.cb_family_live);
        cb_family_message = ViewUtil.findView(this, R.id.cb_family_message);
        if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
            final FamilyUserInfo.FamilyUserBean familyUserBean = FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser();
            cb_family_feed.setChecked(familyUserBean.getSyncFeed() == 1);
            cb_family_shortlive.setChecked(familyUserBean.getSyncShortLive() == 1);
            cb_family_live.setChecked(familyUserBean.getSyncLive() == 1);
//            refreshFamilyMute(familyUserBean.getTid());
            cb_family_feed.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requetPermission();
                }
            });
            cb_family_shortlive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requetPermission();
                }
            });
            cb_family_live.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requetPermission();
                }
            });
            cb_family_message.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setFamilyMute(familyUserBean.getTid());
                }
            });
        }
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(requestPermissionCommand)) {
            Result<JSONObject> result = JSON.parseObject(data, new TypeReference<Result<JSONObject>>() {
            }.getType());
            if (result.data.containsKey("familyUser")) {
                FamilyUserInfo.FamilyUserBean userBean = JSON.parseObject(result.data.getString("familyUser"), FamilyUserInfo.FamilyUserBean.class);
                FamilyAccountManager.getInstance().getFamilyUserInfo().setFamilyUser(userBean);
                cb_family_feed.setChecked(userBean.getSyncFeed() == 1);
                cb_family_shortlive.setChecked(userBean.getSyncShortLive() == 1);
                cb_family_live.setChecked(userBean.getSyncLive() == 1);
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        if (command.equals(requestPermissionCommand)) {
            if (FamilyAccountManager.getInstance().isHasFamilyUserInfo()) {
                final FamilyUserInfo.FamilyUserBean familyUserBean = FamilyAccountManager.getInstance().getFamilyUserInfo().getFamilyUser();
                cb_family_feed.setChecked(familyUserBean.getSyncFeed() == 1);
                cb_family_shortlive.setChecked(familyUserBean.getSyncShortLive() == 1);
                cb_family_live.setChecked(familyUserBean.getSyncLive() == 1);
            }
        }
    }

    private void requetPermission() {
        int feed = cb_family_feed.isChecked() ? 1 : 0;
        int shortLive = cb_family_shortlive.isChecked() ? 1 : 0;
        int live = cb_family_live.isChecked() ? 1 : 0;
        ProtoRequestHelper.cancelRequest(this.hashCode());
        requestPermissionCommand = ProtoRequestHelper.task(ProtoHttpFactory.syncSetting(feed, shortLive, live)).setTag(this.hashCode()).request(this);
    }

    private void refreshFamilyMute(String familyId) {
        Team team = TeamDataCache.getInstance().getTeamById(familyId);
        if (team != null) {
            cb_family_message.setChecked(!team.mute());
        }


    }

    private void setFamilyMute(final String familyId) {
        Team team = TeamDataCache.getInstance().getTeamById(familyId);
        if (team == null) return;
        NIMClient.getService(TeamService.class).muteTeam(familyId, !team.mute()).setCallback(new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void param) {
                refreshFamilyMute(familyId);
            }

            @Override
            public void onFailed(int code) {
                refreshFamilyMute(familyId);
            }

            @Override
            public void onException(Throwable exception) {
                refreshFamilyMute(familyId);
            }
        });
    }
}
